﻿using UnityEngine;
using System.Collections.Generic;

public class LineTool : MonoBehaviour
{
    public static LineTool Instance { get; private set; }

    public GameObject lineRendererPrefab;
    private LineRenderer originLineRenderer;

    private int lineRendererNum;
    private List<LineRenderer> lineRenderers = new List<LineRenderer>();

    private void Awake()
    {
        Instance = this;
        originLineRenderer = lineRendererPrefab.GetComponent<LineRenderer>();
    }

    public LineRenderer requireLineRenderer()
    {
        if (lineRendererNum >= lineRenderers.Count)
        {
            GameObject go = Instantiate(lineRendererPrefab, transform, false);
            go.name = "LineRender " + lineRendererNum;
            LineRenderer renderer = go.GetComponent<LineRenderer>();
            renderer.sortingOrder = lineRendererNum;
            lineRenderers.Add(renderer);
        }
        LineRenderer lineRenderer = lineRenderers[lineRendererNum++];
        lineRenderer.startWidth = originLineRenderer.startWidth;
        lineRenderer.endWidth = originLineRenderer.endWidth;
        return lineRenderer;
    }

    public void BeginDraw()
    {
        lineRendererNum = 0;
    }

    /// <summary>
    /// 绘制单条线
    /// </summary>
    /// <param name="a"></param>
    /// <param name="b"></param>
    /// <param name="color"></param>
    /// <returns></returns>
    public LineRenderer DrawLine(Vector2 a, Vector2 b, Color color)
    {
        return DrawLine(a, b, color, color);
    }
    /// <summary>
    /// 绘制两条线
    /// </summary>
    /// <param name="a"></param>
    /// <param name="b"></param>
    /// <param name="colorA"></param>
    /// <param name="colorB"></param>
    /// <returns></returns>
    public LineRenderer DrawLine(Vector2 a, Vector2 b, Color colorA, Color colorB)
    {
        LineRenderer lineRenderer = requireLineRenderer();
        lineRenderer.startColor = colorA;
        lineRenderer.endColor = colorB;

        lineRenderer.positionCount = 2;
        lineRenderer.SetPosition(0, a);

        if (Vector2.Distance(a, b) > float.Epsilon)
        {
            lineRenderer.SetPosition(1, b);
        }
        else
        {
            lineRenderer.SetPosition(1, b + new Vector2(0.05f, 0));
        }
        return lineRenderer;
    }
    /// <summary>
    /// 绘制平面几何体
    /// </summary>
    /// <param name="vertices"></param>
    /// <param name="color"></param>
    /// <returns></returns>
    public LineRenderer DrawPolygon(List<Vector2> vertices, Color color, float width = 0.05f)
    {
        int n = vertices.Count;
        if (n == 0)
        {
            return null;
        }
        LineRenderer lineRenderer = requireLineRenderer();
        lineRenderer.startColor = color;
        lineRenderer.endColor = color;
        lineRenderer.startWidth = width;
        lineRenderer.endWidth = width;
        lineRenderer.positionCount = n + 1;
        for (int i = 0; i < n; ++i)
        {
            lineRenderer.SetPosition(i, vertices[i]);
        }
        if (n == 1)
        {
            lineRenderer.SetPosition(n, vertices[0] + new Vector2(0.05f, 0));
        }
        else
        {
            lineRenderer.SetPosition(n, vertices[0]);
        }
        return lineRenderer;
    }

    public void EndDraw()
    {
        for (int i = 0; i < lineRenderers.Count; ++i)
        {
            lineRenderers[i].enabled = i < lineRendererNum;
        }
    }
}